package com.it.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Data
@TableName("t_blog")
public class BlogEntity implements Serializable {

    @TableId(type = IdType.AUTO)
    private Long id;
    private String title;
    private String content;

    private String firstPicture;
    private String flag;
    private Integer views;
    private boolean appreciation;

    private boolean shareStatement;
    private boolean commentabled;

    private boolean published;
    private boolean recommend;

    private Date createTime;

    private Date updateTime;

    private Long typeId;

    @TableField(exist = false)
    private BlogTypeEntity type;

    @TableField(exist = false)
    private List<BlogTagEntity> tags = new ArrayList<>();

    private Long userId;
    @TableField(exist = false)
    private UserEntity user;

    @TableField(exist = false)
    private List<BlogCommentEntity> comment = new ArrayList<>();

    @TableField(exist = false)
    private String tagIds;

    private String description;

    public void init() {
        this.tagIds = tagsToIds(this.getTags());
    }


    /**
     * 将数组转化为逗号分割的字符串
     */
    private String tagsToIds(List<BlogTagEntity> blogTagEntities) {
        if (!blogTagEntities.isEmpty()) {
            StringBuffer ids = new StringBuffer();
            boolean flag = false;
            for (BlogTagEntity blogTagEntity : blogTagEntities) {
                if (flag) {
                    ids.append(",");
                } else {
                    flag = true;
                }
                ids.append(blogTagEntity.getId());
            }
            return ids.toString();
        } else {
            return tagIds;
        }
    }

}
